//格式化
function formatterRole(value, row, index) {

    if (value) {
        return value.name;
    }
}
function formatterShop(value, row, index) {
    if (value) {
        return value.name;
    }
}
$(function () {
    var employee_datagrid = $("#employee_datagrid");
    var employee_dialog = $("#employee_dialog");
    var employee_add_form = $("#employee_add_form");
    var combobox_role = $("#combobox_role");

    employee_datagrid.datagrid({
        url: '/employee/list',
        fit: true,
        fitColumns: true,
        pagination: true,
        rownumbers: true,
        singleSelect: true,
        columns: [
            [
                {field: 'name', title: '姓名', width: 100},
                {field: 'age', title: '年龄', width: 100},
                {field: 'role', title: '岗位', width: 100, formatter: formatterRole},
                {field: 'tel', title: '电话', width: 100},
                {field: 'salary', title: '薪酬', width: 100},
                {field: 'shop', title: '店铺', width: 100, formatter: formatterShop},
                {field: 'vdate', title: '入职时间', width: 100},
                {field: 'inputTime', title: '添加时间', width: 100},
            ]
        ]
    })
    employee_dialog.dialog({
        width: 320,
        height: 320,
        closed: true,
        onClose: function () {
            employee_add_form.form('clear')
        }
    })

    var methodObj = {
        add: function () {
            employee_dialog.dialog('open');
            employee_dialog.dialog('setTitle', '新增员工');

        },
        edit: function () {
            var row = employee_datagrid.datagrid('getSelected');
            console.log(row);
            if (!row) {
                $.messager.alert('温馨提示', '请选择一条数据', 'warning');
                return;
            }
            //如果有对象字段,要多添加一条属性
            if (row.shop) {
                row['shop.id'] = row.shop.id;
            }
            //如果有对象字段,要多添加一条属性
            if (row.role) {
                row['role.id'] = row.role.id;
            }
            employee_add_form.form('load', row);
            employee_dialog.dialog('open');
            employee_dialog.dialog('setTitle', '编辑员工');
        },

        reload: function () {
            employee_datagrid.datagrid('reload');
        },
        remove: function () {
            var selectedRow = employee_datagrid.datagrid("getSelected");

            //判断是否选中行
            if (!selectedRow) {
                $.messager.alert("提示", "请选择一行数据", "error");
                return;
            }
            //给出提示确认要删除吗
            $.messager.confirm("提示", "您确定要删除吗?", function (yes) {
                if (yes) {

                    //发送ajax请求执行删除
                    $.post("/employee/delete", {id: selectedRow.id}, function (data) {
                        if (data.success) {
                            $.messager.alert("提示", "操作成功", "info", function () {
                                methodObj.reloadEmp();

                            });
                        } else {
                            $.messager.alert("提示", data.msg, "error");
                        }
                    });
                }
            })
        },
        cancel: function () {
            employee_dialog.dialog('close')
        },
        //提交保存
        save: function () {
            var url = "/employee/save";

            //获取员工的id
            var employeeId = $("#employee_id").val();
            if (employeeId) {
                //编辑的url
                url = "/employee/update";
            }
            //提交表单
            employee_add_form.form("submit", {
                url: url,
                success: function (data) {
                    data = $.parseJSON(data);
                    if (data.success) {
                        $.messager.alert("提示", "操作成功", "info", function () {
                            methodObj.cancel();
                            methodObj.reload();
                        });
                    } else {
                        $.messager.alert("提示", data.msg, "error");
                    }
                }
            })
        }
    }
    $("a[data-cmd]").click(function () {
        var methodName = $(this).data("cmd");
        methodObj[methodName]();
    })
})

